package com.zretc.kscxmz.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.kscxmz.entity.tongxunlu.Contacts;
import com.zretc.kscxmz.mappers.ContactsMapper;
import com.zretc.kscxmz.services.ContactsService;
import com.zretc.kscxmz.util.ChineseCharacterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ContactsServiceImpl implements ContactsService {

    @Autowired
    private ContactsMapper contactsMapper;

    @Override
    public PageInfo<Contacts> getContactsByPage(String accountName,int pageNum, int pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        List<Contacts> list = new ArrayList<>();
        if ("".equals(accountName)){
            accountName=null;
            list = contactsMapper.findByAccountName(accountName);
        }else {
            list = contactsMapper.findByAccountName("%"+accountName+"%");
        }


        PageInfo<Contacts> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    @Override
    public PageInfo<Contacts> getContactsByLetter(String letter, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        String firstName="";
        String firstLetter = "";
        List<Contacts> list = new ArrayList<>();
        PageInfo<Contacts> pageInfo = null;
        List<Contacts> contactsList = contactsMapper.findByLetter();
        if ("ALL".equals(letter)){
           pageInfo = new PageInfo<>(contactsList);
        }else {
            for (Contacts contacts : contactsList) {
                firstName = contacts.getAccountName().substring(0, 1);
                firstLetter = ChineseCharacterUtil.convertHanzi2Pinyin(firstName, false).toUpperCase();
                if (letter.equals(firstLetter)) {
                    list.add(contacts);
                }
            }
            pageInfo = new PageInfo<>(list);
        }


        return pageInfo;
    }
}
